﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;

namespace EdBusTracker.V2
{
    public partial class DisplayBusTimes : PhoneApplicationPage
    {
        BusStops bus_stops;
        String stopCode;

        public DisplayBusTimes()
        {
            InitializeComponent();
            doGetBusTimes();
        }

        private void buttonBack_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void buttonAddFavorite_Click(object sender, RoutedEventArgs e)
        {
            String msg;
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            using (var file = appStorage.OpenFile("favs.txt", FileMode.Append))
            using (var writer = new StreamWriter(file))
            {
                String stopname = bus_stops.getStopName(stopCode);
                if (bus_stops.getFavs().ContainsKey(stopname))
                {
                    msg = "Stop Already Added!";
                }
                else
                {
                    writer.Write(stopCode + "\n");
                    bus_stops.getFavs().Add(stopname, stopCode);
                    msg = "Stop Added!";
                }
            }
            MessageBox.Show(msg);
        }

        private void doRequest(String uri)
        {
            var webClient = new WebClient();
            webClient.OpenReadAsync(new Uri(uri));
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
        }

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            using (var reader = new StreamReader(e.Result))
            {
                String xhtmlString = reader.ReadToEnd();
                int start = xhtmlString.IndexOf("<table");
                int end = xhtmlString.IndexOf("</table>") + 8;
                xhtmlString = xhtmlString.Substring(start, end - start);
                List<string> datalist = bus_stops.parseBusTimes(xhtmlString);
                textBlockTimes.Text = "";
                textBlockServices.Text = "";
                foreach (string s in datalist)
                {
                    String[] data = s.Split(',');
                    if (data.Count() == 5)
                    {
                        textBlockServices.Text += " " + data[0] + "\n";
                        textBlockDest.Text += data[1] + "\n";
                        textBlockTimes.Text += data[2] + "\n";
                    }
                }
                customIndeterminateProgressBar.Visibility = Visibility.Collapsed;
            }
        }

        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            customIndeterminateProgressBar.Visibility = Visibility.Visible;
            doGetBusTimes();
        }

        private void doGetBusTimes()
        {
            textBlockDest.Text  = "";
            textBlockServices.Text = "";
            textBlockTimes.Text = "";
            bus_stops = (App.Current as App).busStops;
            stopCode = bus_stops.stopCode;
            String uri = bus_stops.getStopURL(stopCode);
            doRequest(uri);
        }
    }
}
